﻿namespace ChessToys
{
    using System;
    using System.Collections.Generic;
    
    /// <summary>
    /// <see cref="IMoveGenerator"/> implementation delegating to specific piece dependend <see cref="IMoveGenerator"/>
    /// implementations depending of the piece at the start position.
    /// </summary>
    public class DelegatingPieceMoveGenerator : AbstractMoveGenerator
    {
        private static IDictionary<Type, IMoveGenerator> MOVE_GENERATORS = new Dictionary<Type, IMoveGenerator>();

        static DelegatingPieceMoveGenerator()
        {
            MOVE_GENERATORS.Add(typeof(Pawn), new PawnMoveGenerator());
            MOVE_GENERATORS.Add(typeof(Rock), new RockMoveGenerator());
            MOVE_GENERATORS.Add(typeof(Knight), new KnightMoveGenerator());
            MOVE_GENERATORS.Add(typeof(Bishop), new BishopMoveGenerator());
            MOVE_GENERATORS.Add(typeof(Queen), new QueenMoveGenerator());
            MOVE_GENERATORS.Add(typeof(King), new KingMoveGenerator());
        }

        /// <inheritdoc />
        protected override ISet<Move> GenerateForPiece(Board board, Position start, IPiece piece)
        {
            IMoveGenerator moveGenerator = MOVE_GENERATORS[piece.GetType()];
            if (moveGenerator == null)
            {
                throw new ArgumentException(String.Format("Can not generate moves for {0}", piece));
            }
            return moveGenerator.Generate(board, start);
        }
    }
}
